// pages/liuyan/liuyan.js
const app = getApp();
var router = require("../../router.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [      
    ],
    libottom: [    
    ],
    page: 0,
    info: false, 
  },
  //分享
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      var that = this;
      // 来自页面内转发按钮    
      var id = that.data.id;         
      return {
        title: '自定义转发标题',
        path: '/page/liuyan/liuyan',
        success: function (res) {
          app.request({
            url: router.msg.share,
            method: "POST",
            data: {
              id: id,
            },
            success: function (data) {
              if (data) {
                var list = that.data.list;
                var share = list.share;
                list.share = share + 1;
                that.setData({
                  list: list
                })
                wx.showToast({
                  title: '分享成功',
                  icon: 'success',
                  duration: 2000
                });
              } else {
                wx.showToast({
                  title: '分享失败请重试',
                  icon: 'none',
                  duration: 2000
                });
              }
            }
          });
        },
        fail: function (res) {
          wx.showToast({
            title: '分享已取消',
            icon: 'none',
            duration: 2000
          });
        }
      }
    }
  },

  //点赞
  toCollectother: function (event) {
    var that = this;
    var id = that.data.id; 
    app.request({
      url: router.msg.praise,
      method: "POST",
      data: {
        uid: wx.getStorageSync('uid'),
        id: id,
      },
      success: function (data) {
        if (data) {
          var list = that.data.list;
          var status = list.praise_type;
          list.praise_type = !status;
          var praise = list.praise;
          if (status) {
            list.praise = praise + 1;
          } else {
            var newPraise = praise - 1;
            if (newPraise < 0) {
              newPraise = 0;
            }
            list.praise = newPraise;
          }
          that.setData({
            list: list
          })
          // 交互反馈
          wx.showToast({
            title: list.praise_type ? '取消' : '点赞',
            icon: 'success',
            duration: 2000
          });
        } else {
          wx.showToast({
            title: '操作失败请重试',
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
 
  start_liuyan: function () {
    var id = this.data.id;
    app.navTo('../huifu/huifu?id='+id)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {    
    app.isLogin();
    wx.showShareMenu({
      withShareTicket: true
    })
    this.setData({
      height: wx.getSystemInfoSync().windowHeight,
      id: option.id
    });
    this.getInfo(option.id);   
  },

  lower: function (e) {
    var id = this.data.id;
    this.getInfo(id);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showShareMenu({
      withShareTicket: true
    })  
  },
  getInfo: function (id) {
    var that = this;
    app.request({
      url: router.msg.comment,
      method:"POST",
      data: {
        uid: wx.getStorageSync('uid'),
        page: that.data.page,
        id: id
      },
      success: function (data) {       
        if (that.data.page == 0 && data.say_list){          
          that.setData({
            list: data.say_list,          
          });
        }
        if (data.com_list.length > 0) {
          for (var i = 0; i < data.com_list.length; i++) {            
            that.data.libottom.push(data.com_list[i]);          
          }
          that.setData({
            libottom: that.data.libottom,
            page: that.data.page + 1
          });
        } else {
          that.setData({
            info: true,
          });
        }      
      }
    })
  }
})